# coding:utf-8

from flask.ext.wtf import Form
from wtforms import StringField, PasswordField, SubmitField, BooleanField, TextAreaField, SelectField
from wtforms import ValidationError
from wtforms.validators import DataRequired, Email, Length, Regexp, EqualTo
from project.auth.models import *


class LoginForm(Form):
    username = StringField('用户名', validators=[DataRequired()])
    password = PasswordField('密码', validators=[DataRequired()])
    keep_login = BooleanField('自动登录')
    submit = SubmitField('登录')


class RegForm(Form):
    email = StringField('邮箱', validators=[DataRequired('邮箱不能为空'), Email(message='邮箱格式错误')])
    username = StringField('用户名', validators=[DataRequired('用户名不能为空'), Length(1, 64),
                                              Regexp('^[A-Za-z][A-Za-z0-9_]*$', 0, '用户名必须由数字、字母、下划线组成，且必须字母开头')])
    password = PasswordField('密码', validators={DataRequired('密码不能为空')})
    password2 = PasswordField('确认密码', validators=[EqualTo('password', message='确认密码错误')])
    submit = SubmitField('注册')

    def validate_email(self, field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('邮箱已存在')

    def validate_username(self, field):
        if User.query.filter_by(username=field.data).first():
            raise ValidationError('用户名已存在')
